﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Cache;
using YasharEl.Infrastructure.Caching.Orm;
using YasharEl.Infrastructure.ObjectFactory;

namespace YasharEl.Infrastructure.Caching.NHibernate.Integration
{
    public class NHibernateOrmCacheProvider : ICacheProvider
    {
        #region ICacheProvider Implementations

        /// <summary>
        /// Configure the cache
        /// </summary>
        /// <param name="regionName">the name of the cache region</param>
        /// <param name="properties">configuration settings</param>
        /// <returns></returns>
        public ICache BuildCache(string regionName, IDictionary<string, string> properties)
        {
            // TODO : Refactor DependencyContainer static method call here

            return new NHibernateOrmCacheAdapter(
                DependencyContainer.Resolve<IOrmCacheManager>(),
                regionName
            );
        }

        public long NextTimestamp()
        {
            return Timestamper.Next();
        }

        /// <summary>
        /// Callback to perform any necessary initialization of the underlying cache implementation
        /// during ISessionFactory construction.
        /// </summary>
        /// <param name="properties">current configuration settings</param>
        public void Start(IDictionary<string, string> properties)
        {
        }

        /// <summary>
        /// Callback to perform any necessary cleanup of the underlying cache implementation
        /// during <see cref="ISessionFactory.Close" />.
        /// </summary>
        public void Stop()
        {
        }

        #endregion
    }
}
